#include<bits/stdc++.h>
#define int long long
using namespace std;
namespace IO
{
	char buff[1<<21],*p1=buff,*p2=buff;
	char getch(){return p1==p2&&(p2=((p1=buff)+fread(buff,1,1<<21,stdin)),p1==p2)?EOF:*p1++;}
	template<typename T>void read(T &x){char CH=getch();int fl=1;x=0;while(CH>'9'||CH<'0'){if(CH=='-')fl=-1;CH=getch();}while(CH<='9'&&CH>='0'){x=x*10+CH-48;CH=getch();}x*=fl;}
	template<typename T>void read_s(T &x){x="";char CH=getch();while(!(CH>='a'&&CH<='z')&&!(CH>='A'&&CH<='Z'))CH=getch();while((CH>='a'&&CH<='z')||(CH>='A'&&CH<='Z')){x+=CH;CH=getch();}}
	template<typename T,typename ...Args>void read(T &x,Args& ...args){read(x);read(args...);}
	char obuf[1<<21],*p3=obuf;
	void putch(char CH) {if(p3-obuf<(1<<21))*p3++=CH;else fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=CH;}
	char CH[100];
	template<typename T>void write(T x) {if(!x)return putch('0');if(x<0)putch('-'),x*=-1;int top=0;while(x)CH[++top]=x%10+48,x/=10;while(top)putch(CH[top]),top--;}
	template<typename T,typename ...Args>void write(T x,Args ...args) {write(x);putch(' ');write(args...);}
	void put(string s){for(int i=0;i<s.size();i++)putch(s[i]);}
	void flush(){fwrite(obuf,p3-obuf,1,stdout);}
}
using namespace IO;
int n,m;
int a[500005],val[500005];
int bkt[500005];
#define pii pair<int,int>
#define fi first
#define se second
int tot;
const int inf=INT_MAX;
namespace Shiki
{
	pii t[500005];
	inline int lowbit(int x){return x&-x;}
	inline void add(int x,pii v){for(;x<=tot;x+=lowbit(x))t[x]=max(t[x],v);}
	inline pii query(int x){pii res={-inf,-inf};for(;x;x-=lowbit(x))res=max(res,t[x]);return res;}
}using namespace Shiki;
pii f[500005];
inline int check(int x)
{
	for(int i=0;i<=tot;i++) t[i]={-inf,-inf};
	for(int i=1;i<=n;i++)
	{
		if(val[a[i]]>x) continue;
		f[i]={1,val[a[i]]};
		int p=upper_bound(bkt+1,bkt+tot+1,x-val[a[i]])-bkt-1;
		pii tmp=query(p);
		f[i]=max(f[i],{tmp.fi+1,-tmp.se});
		add(a[i],{f[i].fi,-f[i].se});
		if(f[i].fi==m&&f[i].se+val[a[i]]<=x) return 1;
	}
	return 0;
}
signed main()
{
	freopen("sub.in","r",stdin);
	freopen("sub.out","w",stdout);
	read(n,m);
	for(int i=1;i<=n;i++) read(a[i]),bkt[i]=a[i];
	sort(bkt+1,bkt+n+1);
	tot=unique(bkt+1,bkt+n+1)-bkt-1;
	bkt[++tot]=inf+5;
	for(int i=1;i<=n;i++) 
	{
		int x=a[i];
		a[i]=lower_bound(bkt+1,bkt+tot+1,a[i])-bkt;
		val[a[i]]=x;
	}
	int l=0,r=2e9,ans=0;
	while(l<=r)
	{
		int mid=l+r>>1;
		if(check(mid)) r=mid-1,ans=mid;
		else l=mid+1;
	}
	write(ans);
	flush();
	return 0;
}
